/* Model generation and testing */
clear

/*set your path to the appropriate folder here */
cd "D:\Dropbox\Research Papers\Military Deployments and Coups\Data" 

log using final_replication, replace

use primary_coup_replication.dta
tsset ccode1 year

/*base models */
set more off
eststo clear

tsset ccode1 year
set emptycells drop
eststo: logit coup l.lntroops100 i.regime_case_fe, robust
set emptycells drop
eststo: logit coup l.lntroops100 l.excluded l.ln_terrorist_attack l.civil_war l.lnmilex l.lnmilper l.marxist l.coldwar i.regime_case_fe, robust
set emptycells drop
eststo: logit coup l.lntroops100 l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper l.lnrussian_troops l.coldwar i.regime_case_fe, robust
set emptycells drop
eststo: logit coup l.lntroops100 l.dependence l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper l.marxist l.coldwar i.regime_case_fe, robust
eststo: logit coup l.lntroops100 l.kappavv l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar i.regime_case_fe, robust
set emptycells drop
eststo: logit coup l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe, robust

esttab _all using ..\Tables\Basic_Results_RR.tex, replace ///
	mtitles("Naive" "Domestic Controls" "International Controls" "Trade Dependence" "US Similarity" "Cubic Polynomial")   ///
	scalars("r2_p Pseudo $ R^2$" N) style(tex)   b(%9.3gc) obslast label se nogaps  ///
	star(* 0.10 ** 0.05 *** 0.01)  alignment(S) drop(*.regime_case_fe) ///
	compress nonotes eqlabels(none) ///
	addnote("Two-tailed tests reported for all variables; robust standard errors in parentheses." ///
	"Fixed effects results not reported. $*$ p$\leq$ 0.10; $**$ p$\leq$ 0.05; $***$ p$\leq$0.01.") ///
		substitute(L_ "" L. "")


/* Spatial controls */
set more off
eststo clear
tsset ccode1 year
set emptycells drop
eststo: logit coup l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe l.sp_coup,  robust
set emptycells drop
eststo: logit coup l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe l.sp_lntroops100,  robust



esttab _all using ..\Tables\Spatial_Results_RR.tex, replace ///
	mtitles("Spatial Coups" "Spatial Troops")   ///
	scalars("r2_p Pseudo $ R^2$" N) style(tex)   b(%9.3gc) obslast label se nogaps  ///
	star(* 0.10 ** 0.05 *** 0.01)  alignment(S) drop(*.regime_case_fe) ///
	compress nonotes eqlabels(none)  ///
	addnote("Two-tailed tests reported for all variables; robust standard errors in parentheses. " ///
	"Fixed effects results not reported. $*$ p$\leq$ 0.10; $**$ p$\leq$ 0.05; $***$ p$\leq$0.01.") ///
		substitute(L_ "" L. "")
		

/* Coup types */
set more off
eststo clear
tsset ccode1 year
set emptycells drop
quietly eststo: logit unrealized l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit realized l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit conspiracy l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit attempt l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit military l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit dissident l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop

set emptycells drop
esttab _all using ..\Tables\Coup_Types_Results_RR_1.tex, replace ///
	mtitles("Unrealized" "Realized" "Conspiracy" "Attempt" "Military" "Dissident" )   ///
	scalars("r2_p Pseudo $ R^2$" N) style(tex)   b(%9.3gc) obslast label se nogaps  ///
	star(* 0.10 ** 0.05 *** 0.01)  alignment(S)  drop(*.regime_case_fe) ///
	compress nonotes eqlabels(none) ///
	addnote("Two-tailed tests reported for all variables; robust standard errors in parentheses." ///
	"Fixed effects results not reported. $*$ p$\leq$ 0.10; $**$ p$\leq$ 0.05; $***$ p$\leq$0.01.") ///
		substitute(L_ "" L. "")
set emptycells drop		


set more off
eststo clear
quietly eststo: logit rebel l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit palace l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit foreign l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit auto l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit resign l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop
quietly eststo: logit popular l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe,  robust
set emptycells drop



set emptycells drop
esttab _all using ..\Tables\Coup_Types_Results_RR_2.tex, replace ///
	mtitles("Rebel" "Palace" "Foreign" "Auto" "Resign" "Popular" "Counter")   ///
	scalars("r2_p Pseudo $ R^2$" N) style(tex)   b(%9.3gc) obslast label se nogaps  ///
	star(* 0.10 ** 0.05 *** 0.01)  alignment(S)  drop(*.regime_case_fe) ///
	compress nonotes eqlabels(none) ///
	addnote("Two-tailed tests reported for all variables; robust standard errors in parentheses." ///
	"Fixed effects results not reported. $*$ p$\leq$ 0.10; $**$ p$\leq$ 0.05; $***$ p$\leq$0.01.") ///
		substitute(L_ "" L. "")
set emptycells drop		

/*Run below separately from the rest of the models. For some reason, this produces a blank graph when run with the rest */

 tsset ccode1 year
logit coup l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 i.regime_case_fe, robust
sum l.lntroops100 l.dependence l.kappavv  l.threat_env l.excluded l.ln_terrorist_attack l.civil_war  l.lnmilex l.lnmilper  l.marxist l.coldwar l.time l.time2 l.time3 if e(sample)
margins, at(l.lntroops100=(0(.5)11.3) l.coldwar=(0 1))
marginsplot,  level(95) graphregion(color(white)) bgcolor(white) ytitle("Probability of Coup Event") xlabel(0(3)12) xtitle("ln(troop count)") legend(order(1 "Post-Cold War" 2 "Cold War")) title("") recast(line) recastci(rarea)  ciopts(color(%60) fintensity(30)) 
graph export t1m2.png, replace	


			
			
			